package org.lanqiao.tjut.control;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserLoginControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;

	/*
	 * 该注解： 当调用handler方式时，会进行执行，完成一些初始化处理工作
	 * 模块级别的自定义类型转换： string --- date
	 */
	/*@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//不做容错处理，例如：false-不把1996-13-3 转换为1997-1-3
		dateFormat.setLenient(false);
		// true:允许输入空值，false:不能为空值
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}*/
	
	@RequestMapping("/login.do")
	public ModelAndView doLogin2(HttpSession session, UsersEntity userE) {
		System.out.println("登录用户名：" + userE.getUsername() + "@" + userE.getUserPsw());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		List<UsersEntity> lstUsers = userService.selectUsersInfo4Login(userE);
		if (lstUsers != null && lstUsers.size() > 0) {
			// 登录成功

			// 登录验证成功，将用户信息进行session缓存
			session.setAttribute("LoginedUserInfo", lstUsers.get(0));
			// 设置逻辑视图名--登录成功
			model.setViewName("commons/logined");
		} else {
			// 设置逻辑视图名--登录验证失败
			model.setViewName("commons/login");
		}
		return model;
	}

	@RequestMapping("/saveUserInfo.do")
	public String saveUserInfo(HttpSession session, UsersEntity userE) {
		System.out.println("登录用户名：" + userE.getUsername() + "@" + userE.getUserPsw());
		MyUtils.logger.debug("birthday:"+MyUtils.convertDate2String(userE.getUserBirthday(), "yyyy-MM-dd HH:mm:ss"));
		return "userInfoManager/userInfoAdd";
	}
}
